package com.tjcu.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tjcu.entity.Msg;
import com.tjcu.entity.User;

import com.tjcu.service.UserService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController//返回json数据
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 依伴汉服后台登录
     *
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @CrossOrigin
    public Msg login(String userName, String password) {
        User user = userService.getUser(userName, password);
        if (user != null) {
            System.out.println("输出" + user);
            long time = System.currentTimeMillis();
            String token = time + userName;
            userService.insertToken(user.getUserId(), token);
            System.out.println(token);
            user.setToken(token);
            return Msg.success().add("user", user);
        } else {
            return Msg.fail();
        }

    }


    @RequestMapping("/getUserInfo")
    public Msg getUserInfo(@RequestBody User user) {
        int id = user.getUserId();
        User user1 = userService.getUserInfo(id);
        return Msg.success().add("user", user1);
    }

    //上传头像并保存到本地 上传可以公用
    @RequestMapping("/upload")
    public Msg upload(MultipartFile file, User user, ModelMap map) throws IOException {
        //图片上传成功后，将图片的地址写到数据库
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        System.out.println("原始图片的名字"+originalFilename);
        String newFileName = UUID.randomUUID() + ".jpg";//不要加原始文件名称了

        CommonsMultipartFile cFile = (CommonsMultipartFile) file;
        DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();



        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tNvGuLgqM8vGhAA1J1s";
        String accessKeySecret = "DnaJMu2OxtwewycJEg5anC62gHto2L";
        String bucketName = "hanfu-img";  //存储空间名

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
        InputStream inputStream = fileItem.getInputStream();
        //上传文件
        ossClient.putObject(bucketName, newFileName, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();


        return Msg.success().add("imgData", newFileName);
    }

    //更新数据库的头像名称
    @RequestMapping("/updateImg")
    public Msg updateImg(@RequestBody User user) {
        int id = user.getUserId();
        String img = user.getImg();
        userService.updateImg(user);
        return Msg.success();

    }


    //更新用户信息
    @RequestMapping("/updateInfo")
    public Msg updateInfo(@RequestBody User user) {
        int id = user.getUserId();
        String nickName = user.getNickName();
        String phone = user.getPhone();
        int gender = user.getGender();
        userService.updateInfo(user);
        return Msg.success();

    }

    //修改密码
    @RequestMapping("/updatePsw")
    public Msg updatePsw(@RequestBody User user, HttpServletRequest request) throws ServletException, IOException {

        int id = user.getUserId();
        String passPast = userService.getPsw(id);
        System.out.println("胡二虎额司法" + passPast);
//        String oldPsw=request.getParameter("oldPsw");
        String oldPsw = user.getOldPsw();
        System.out.println("wawdjafjooo噢噢噢噢" + oldPsw);
        if (oldPsw.equals(passPast)) {
            String password = user.getPassword();
            userService.updatePsw(user);
            return Msg.success();

        } else {
            return Msg.fail();
        }

    }


}
